package top.zhouze.common.base;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.Toast;
import androidx.activity.ComponentActivity;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewbinding.ViewBinding;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import top.zhouze.common.annotation.RegisterBus;
import top.zhouze.common.annotation.Title;
import top.zhouze.common.util.ReflectUtil;
import top.zhouze.common.util.RxUtil;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author 周仄
 * @since 2023/6/26 9:01
 */
public abstract class ViewBindingActivity<V extends ViewBinding> extends AppCompatActivity {
    protected V binding;


    @SuppressLint("MissingSuperCall")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        callSuper(savedInstanceState);
        init();
        //页面接受的参数方法
        initParam();
        //页面数据初始化方法
        initData();
        //页面事件监听的方法，一般用于ViewModel层转到View层的事件注册
        initViewObservable();
    }

    void callSuper(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    void init() {
        Title title = getClass().getAnnotation(Title.class);
        if (title != null) {
            setTitle(title.value());
        }
        initViewDataBinding();
        setContentView(binding.getRoot());
        if (getClass().isAnnotationPresent(RegisterBus.class)) {
            addSubscribe(RxUtil.registerBus(this));
        }
    }


    public void showToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }


    /**
     * 初始化参数
     */
    protected abstract void initParam();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 页面事件监听的方法，一般用于ViewModel层转到View层的事件注册
     */
    protected abstract void initViewObservable();

    @SuppressWarnings({"unchecked"})
    private void initViewDataBinding() {
        Class<V> vCls = ReflectUtil.getGenericClass(this,0);
        try {
            Method method = vCls.getDeclaredMethod("inflate", LayoutInflater.class);
            binding = (V) method.invoke(null, getLayoutInflater());
        }  catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    private CompositeDisposable mCompositeDisposable;

    protected void addSubscribe(Disposable... disposable) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
        mCompositeDisposable.addAll(disposable);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mCompositeDisposable != null && !mCompositeDisposable.isDisposed()) {
            mCompositeDisposable.dispose();
        }
    }

    /**
     * 用于获取LifecycleOwner和Context
     *
     * @return {@link ComponentActivity}
     */
    protected ComponentActivity getThisActivity() {
        return this;
    }
}
